<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">总公司名称</label>

        <el-select
          v-model="query.companyId"
          clearable
          filterable
          placeholder="请选择"
          style="width: 185px"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.company_id"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="100px"
        >
          <el-form-item label="关联总公司">
            <el-select v-model="form.companyId" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.company_id"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <!--               v-model="form.bannerUrl" -->
          <el-form-item label="轮播图路径">
            <el-upload
              :action="fileUploadApi + '?companyId=' + form.companyId"
              :file-list="fileList"
              :limit="3"
              v-model="form.bannerUrl"
              :on-success="handleAvatarSuccess"
              :on-exceed="handleExceed"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
            <!-- <el-input v-model="form.bannerUrl" style="width: 370px" /> -->
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="companyId" label="关联总公司">
          <template slot-scope="scope">
            {{ dict.label.company_id[scope.row.companyId] }}
          </template>
        </el-table-column>
        <el-table-column label="轮播图">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="imgUrl + scope.row.bannerUrl"
            ></el-image>
          </template>
        </el-table-column>

        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="updateTime" label="更新时间" />

        <el-table-column
          v-if="checkPer(['admin', 'banner:edit', 'banner:del'])"
          label="操作"
          width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission" />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudBanner from "@/api/banner/banner.js";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operation";
import pagination from "@crud/Pagination";
import { mapGetters } from "vuex";

const defaultForm = {
  bannerId: null,
  bannerUrl: null,
  createTime: null,
  updateTime: null,
  companyId: null,
};
export default {
  name: "Banner",
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ["company_id"],
  cruds() {
    return CRUD({
      title: "轮播图接口",
      url: "api/banner",
      idField: "bannerId",
      sort: "bannerId,desc",
      crudMethod: { ...crudBanner },
    });
  },
  data() {
    return {
      imgUrl: "https://www.f-cloud.top/web/file/图片/",
      fileUploadApi: "https://www.f-cloud.top/web/api/localStorage/pictures",
      fileList: [],
      permission: {
        add: ["admin", "banner:add"],
        edit: ["admin", "banner:edit"],
        del: ["admin", "banner:del"],
      },
      rules: {
        bannerId: [
          { required: true, message: "主键不能为空", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["baseApi"]),
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.form.bannerUrl = res;
      // defaultForm.bannerUrl = res;
      console.log(this.form);
      console.log("this.form.bannerUrl", this.form.bannerUrl);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件`);
    },
  },
  // 钩子：在获取表格数据之前执行，false 则代表不获取数据
  [CRUD.HOOK.beforeRefresh]() {
    return true;
  },
};
</script>

<style scoped>
</style>
